import edu.princeton.cs.algs4.Stopwatch;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdOut;

public class SortCompare {

    /*
    static private int N = 0;
    static private int T = 0;
    static private String[] algs = new String[20];
    */
    
    public static double time(String alg, Comparable[] a) {

        Stopwatch timer = new Stopwatch();

        if(alg.equals("Insertion"))
            Insertion.sort(a);
        if(alg.equals("Selection"))
            Selection.sort(a);
        if(alg.equals("Shell"))
            Selection.sort(a);
        return timer.elapsedTime();
    }

    public static double timeRandomInput(String alg, int N, int T) {
        
        double total = 0.0;
        Double[] a = new Double[N];

        for(int t = 0; t < T; t++) {

            for(int i = 0; i < N; i++)
                a[i] = StdRandom.uniform();
            total += time(alg, a);
        }

        return total;
    }

    
    static void moreThan3Algs(String[] args) {

        int n = args.length;
        /* java SortCompare alg1 alg2 alg3 xxx xxx ...  N T */
        assert n >= 5;

        String[] algs = new String[n-2];
        /* Repeate T times */
        int T = Integer.parseInt(args[n-1]);
        /* length N */
        int N = Integer.parseInt(args[n-2]);

        for(int i = 0; i < n-2; i++)
        {
            algs[i] = args[i];
        }

        double[] t = new double[n-2];
        for(int i = 0; i < n - 2; i++) {
            t[i] = timeRandomInput(algs[i], N, T);
        }
        StdOut.printf("For %d random Doubles\r\n", N);
        for(int i = 0; i < n-2; i++) {

            StdOut.printf("%s take %.3f S\r\n", algs[i], t[i]);
        }

    }

    static void twoAlgs(String[] args) {

        
        /* java SortCompare alg1 alg2 N T */
        String alg1 = args[0];
        String alg2 = args[1];
        
        int N = Integer.parseInt(args[2]);
        /* Repeate T times */
        int T = Integer.parseInt(args[3]);
        
        double t1 = timeRandomInput(alg1, N, T);
        double t2 = timeRandomInput(alg2, N, T);
        
        StdOut.printf("For %d random Doubles\r\n %s is", N, alg1);
        StdOut.printf(" %.1f times than %s\r\n", t1/t2, alg2);
        
    }

    public static void main(String[] args) {

        int n = args.length;
        
        assert n >= 4;

        if(n == 4)
        {
            twoAlgs(args);
        }
        else
        {
            moreThan3Algs(args);
        }

    }
}
